﻿Imports Global_Visas_Business_Layer
Imports System.ComponentModel
Partial Public Class frmrevertWriteOff

    Private Sub btnRevert_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnRevert.Click

        Dim blnRevert As Boolean = False

        If chkAssignTo.IsChecked = True Then
            If cboList.SelectedIndex = -1 Then
                CommonFuncs.DisplayMessage("Please select a consultant from the list", CommonDialog.CustomDialogIcons.Information, "Select consultant")
                Exit Sub


            End If
        End If

        If chkCaseStage.IsChecked = True Then
            If cboCaseStages.SelectedIndex = -1 Then
                CommonFuncs.DisplayMessage("Please select a case stage from the list", CommonDialog.CustomDialogIcons.Information, "Select consultant")
                Exit Sub
                'CommonFunc.AssignToConsultant(GlobalCRM.CurrentCaseID, cboList.SelectedValue, cboList.Text.ToString)
            End If
        Else
            If MsgBox("Are you sure to continue without setting the case stage?", MsgBoxStyle.YesNo, "Case Stage?") = MsgBoxResult.No Then
                Exit Sub
            End If
        End If


        If chkFinance.IsChecked = True Then
            If CommonDataFunctions.RevertWriteOffAmount(GlobalCRM.CurrentCaseID) = True Then
                blnRevert = True
            Else
                blnRevert = False
            End If
        Else

            blnRevert = True

        End If

        If blnRevert = True Then
            'MsgBox("Case reverted back from Written Off", MsgBoxStyle.Information, "Reverted")
        Else
            CommonFuncs.DisplayMessage("Error when reverting back from Written Off", CommonDialog.CustomDialogIcons.Information, "Error")
            Exit Sub
        End If

        If chkAssignTo.IsChecked = True Then
            If CommonDataFunctions.AssignToConsultant(GlobalCRM.CurrentCaseID, cboList.SelectedValue, cboList.Text.ToString) = True Then
                blnRevert = True
            Else
                blnRevert = False
            End If
        Else
            blnRevert = True
        End If

        If blnRevert = True Then
            'MsgBox("Case reverted back from Written Off", MsgBoxStyle.Information, "Reverted")
        Else
            CommonFuncs.DisplayMessage("Error when reverting back from Written Off", CommonDialog.CustomDialogIcons.Information, "Error")
            Exit Sub
        End If

        If chkCaseStage.IsChecked = True Then
            If CommonDataFunctions.SetCaseStage(GlobalCRM.CurrentCaseID, cboCaseStages.SelectedValue, cboCaseStages.Text.ToString) = True Then
                blnRevert = True
            Else
                blnRevert = False
            End If
        Else
            blnRevert = True
        End If
        'put back to case stage???

        If blnRevert = True Then
            'MsgBox("Case reverted back from Written Off", MsgBoxStyle.Information, "Reverted")
        Else
            CommonFuncs.DisplayMessage("Error when reverting back from Written Off", CommonDialog.CustomDialogIcons.Information, "Error")
            Exit Sub
        End If

        If CommonDataFunctions.RevertWriteOff(GlobalCRM.CurrentCaseID) = True Then
            blnRevert = True
        Else
            blnRevert = False
        End If


        If blnRevert = True Then
            'MsgBox("Case reverted back from Written Off", MsgBoxStyle.Information, "Reverted")
        Else
            CommonFuncs.DisplayMessage("Error when reverting back from Written Off", CommonDialog.CustomDialogIcons.Information, "Error")
            Exit Sub
        End If

        Me.Close()
        If blnRevert = True Then
            CommonFuncs.DisplayMessage("Case reverted back from Written Off", CommonDialog.CustomDialogIcons.Information, "Reverted")
            Dim frmCRMMainDisplay As New frmCRMMainDisplay
            Try
                frmCRMMainDisplay.loadTabEvent = False
                frmCRMMainDisplay.tabCase_Loaded(sender, e)

                'frmCRMMainDisplay.frmMainDisplay_Loaded(sender, e)
            Catch ex As Exception

            End Try
        Else
            CommonFuncs.DisplayMessage("Error when reverting back from Written Off", CommonDialog.CustomDialogIcons.Information, "Error")
            Exit Sub
        End If
    End Sub

    Private Sub frmrevertWriteOff_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        chkFinance.IsChecked = True
        chkAssignTo.IsChecked = True
        chkCaseStage.IsChecked = True
        With Me.cboCaseStages
            .DisplayMemberPath = "Descr"
            .SelectedValuePath = "ID"
            .ItemsSource = CType(CommonDataFunctions.LoadCaseStages(GlobalCRM.CurrentCaseID), ilistsource).getlist
            .SelectedIndex = -1
        End With
    End Sub

    Private Sub chkAssignTo_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles chkAssignTo.Checked, chkAssignTo.Unchecked
        If chkAssignTo.IsChecked = False Then
            rdoConsultant.IsChecked = False
            'rdoCreditController.Checked = False
            rdoConsultant.IsEnabled = False
            'rdoCreditController.Enabled = False
        Else
            rdoConsultant.IsChecked = True
            'rdoCreditController.Checked = True
            rdoConsultant.IsEnabled = True
            'rdoCreditController.Enabled = True
        End If
    End Sub
    Private Sub rdoConsultant_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoConsultant.Checked, rdoConsultant.Unchecked
        If rdoConsultant.IsChecked = True Then
            'load consultant list
            With Me.cboList
                .DisplayMemberPath = "con_name"
                .SelectedValuePath = "con_id"
                .ItemsSource = CType(CommonDataFunctions.ConsultantList, IListSource).GetList
                .SelectedIndex = -1
                'CreditControllerList()
            End With
            cboList.IsEnabled = True
        Else
            cboList.IsEnabled = False
        End If
    End Sub
    Private Sub chkCaseStage_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkCaseStage.Checked, chkCaseStage.Unchecked
        If chkCaseStage.IsChecked = True Then
            cboCaseStages.IsEnabled = True
        Else
            cboCaseStages.IsEnabled = False
        End If
    End Sub
End Class
